﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class OrderItem
    {
        public static int insert(OrderItemEntity _oritem)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_OrderItem_insert", _oritem.CustomerId, _oritem.SellerId, _oritem.ProductId, _oritem.Price,
               _oritem.SellerPrice, _oritem.Unit, _oritem.Quantity, _oritem.Note, _oritem.OrderId, _oritem.InitQuantity,_oritem.BusinessNature));
        }

        public static int insertDynamicOrderItem(OrderItemEntity _oritem)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_DynamicOrderItem_insert", _oritem.CustomerId, _oritem.SellerId, _oritem.ProductId, _oritem.Price,
               _oritem.SellerPrice, _oritem.Unit, _oritem.Quantity, _oritem.Note, _oritem.OrderId, _oritem.InitQuantity, _oritem.BusinessNature));
        }
        public static int update(OrderItemEntity _oritem)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_OrderItem_update", _oritem.CustomerId, _oritem.SellerId, _oritem.ProductId, _oritem.Price,
               _oritem.SellerPrice, _oritem.Unit, _oritem.Quantity, _oritem.Note, _oritem.OrderId, _oritem.InitQuantity,_oritem.BusinessNature));
        }

        public static void updateQuantity(int oiid,decimal quantity,string note)
        {
            DbHelper.ExecuteNonQuery("p_OrderItem_updateQuantity", oiid,quantity,note);
        }
        public static void updateDynamicOrderItem(int orderid, int productid, string businessnature)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrderItem_update", orderid, productid, businessnature);
        }
        public static void updateDynamicOrderItemByOiid(int oiid, string note, decimal quantity)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrderItem_updateByOiid", oiid, note, quantity);
        }
        public static void delete(int oiid)
        {
            DbHelper.ExecuteNonQuery("p_OrderItem_delete", oiid);
        }
        public static void deleteDynamicOrderItem(int oiid)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrderItem_delete", oiid);
        }

        public static DataTable listWithSaleReport(string where)
        {
            return DbHelper.ExecuteDataTable("p_OrderItem_listWithSaleReport", where);
        }

        public static DataSet listwithReceivables(string where)
        {
            return DbHelper.ExecuteReDataSet("p_OrderItem_listwithReceivables", where);
        }

        public static DataSet listwithPayables(string where)
        {
            return DbHelper.ExecuteReDataSet("p_OrderItem_listwithPayables", where);
        }
        public static void updateCurrentCost(int productId,decimal sellerPrice,int adminid,string nature)
        {
            DbHelper.ExecuteNonQuery("p_OrderItem_updateCurrentCost", productId, sellerPrice, adminid, nature);
        }
        public static bool isNotquoted(int orderid)
        {
            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_OrderItem_isNotquoted", orderid)) == 1)
                return true;
            else
                return false;
        }
        public static DataTable listWithPurchaseInfo(int parentid,int categoryid,string productNo,string productName,DateTime? startDt,DateTime? endDt)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_OrderItem_listWithPurchaseInfo]", parentid, categoryid, productNo, productName, startDt, endDt);
        }
        public static DataSet listwithProductGrossprofit(string where, int pageSize, int pageIndex)
        {
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("@where", SqlDbType.VarChar);
            param[0].Value = where;
            param[1] = new SqlParameter("@pageSize", SqlDbType.Int);
            param[1].Value = pageSize;
            param[2] = new SqlParameter("@pageIndex", SqlDbType.Int);
            param[2].Value = pageIndex;
            return DbHelper.ExecuteReDataSet("p_OrderItem_listwithProductGrossprofit", param);
        }
        public static DataSet listwithCategoryGrossprofit(string where, int pageSize, int pageIndex)
        {
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("@where", SqlDbType.VarChar);
            param[0].Value = where;
            param[1] = new SqlParameter("@pageSize", SqlDbType.Int);
            param[1].Value = pageSize;
            param[2] = new SqlParameter("@pageIndex", SqlDbType.Int);
            param[2].Value = pageIndex;
            return DbHelper.ExecuteReDataSet("p_OrderItem_listwithCategroyGrossprofit", param);
        }
    }
}
